<?php

namespace Biz\service\bizActivity\base;

use Biz\cache\brand\Brand as cBrand;
use Doraemon\cache\Activity\WechatWebLogin;
use Doraemon\pockets\action\ApiAction;

class Util
{

    const IS_TEST = false;
    const MIN_APP_VERSION = '5.8.1';
    //活动有效期
    const ACTIVITY_TIME_START = '2022-03-21 00:00:00'; //开始时间
    const ACTIVITY_TIME_END = '2022-04-02 00:00:00';  //结束时间

    public static function isWechatUser($openid): bool
    {
        return WechatWebLogin::isExistWechatUser($openid) > 0;
    }

    /**
     * 判断是否为测试环境
     * @return int
     */
    public static function isTest()
    {
        return static::IS_TEST ? 1 : 0;
    }

    /**
     * 检查app版本是否可用
     * @param $currentVersion
     * @return bool
     */
    public static function versionCheck($currentVersion): bool
    {
        return version_compare($currentVersion, static::MIN_APP_VERSION, ">=");
    }

    /**
     * 限制提交频率
     * @param $key
     * @return bool
     */
    public static function lockSubmit($lockKey, $localTime = 3)
    {
        if (cBrand::isLock($lockKey, $localTime)) {
            throw new \Exception("请勿重复提交！", ApiAction::CODE_ERROR_VIEW_TOSTA);
        }
    }


    /**
     * 获取活动内容展示时间
     * @return string
     */
    public static function getContentShowDate()
    {
        $date = date('Y-m-d', time());
        $activityBeginTime = strtotime(static::ACTIVITY_TIME_START);
        if (time() < $activityBeginTime) {
            $date = date('Y-m-d', $activityBeginTime);
        }
        $activityEndTime = strtotime(static::ACTIVITY_TIME_END);
        if (time() >= $activityEndTime) {
            $date = date('Y-m-d', $activityEndTime);
        }
        return $date;
    }

}